﻿$(document).ready(function(){
	/*** 初始化 ***/
	//ajax请求时的加载状态
	$(window).ajaxSend(function(evt, request, settings){
		look.loader.show();
	}).ajaxComplete(function(event, request, settings){
		look.loader.hide();
		publicInit.i18n.Render();
	});
	
	//监视hash
	try{
		$(window).hashchange(function(){
			look.loadFn();
		});
		$(window).hashchange();
	}catch(e){}
	
	$(window).resize(function(){
		publicInit.layout();
	});
	publicInit.layout();
});

var publicInit = {
	headerLoader : function(){
		if($("#header").children().length == 0){
			loadPage("#header","header.html");
			publicInit.i18n.ChangeManual();
		}
		$("#exitSubset").attr("href", "http://106.187.89.39:8010/action/user,logOut.php?backUrl=" + look._SITE_DOMAIN + "/local/user/login");
	},
	menuLoader : function(){
		var menuHtml = '';
		var getMenuCallback = function(back){
			var url = "",type = "",theme = "",dream = "";
			for(var i = 0; i < back.data.length; i++){
				menuHtml += '<li><a href="javascript:">' + back.data[i].name + '</a></li><ul show="true">';
				for(var j = 0; j < back.data[i].subMenus.length; j++){
					if(back.data[i].subMenus[j].url){
						url = back.data[i].subMenus[j].url;
						theme = look.getStringParam("theme", url) ? look.getStringParam("theme", url) : "";
						type = look.getStringParam("type", url) ? look.getStringParam("type", url) : "";
						dream = look.getStringParam("dream", url) ? look.getStringParam("dream", url) : "";
					}else{
						url = "javascript:";theme = "";type = "";dream = "";
					}
					menuHtml += '<li><a href="' + url + '" theme="' + theme + '" type="' + type + '" dream="' + dream + '">' + back.data[i].subMenus[j].name + '</a></li>';
					if (back.data[i].subMenus[j].subMenus) {
						if (back.data[i].subMenus[j].subMenus.length > 0) {
							menuHtml += "<ul>";
							for (var k = 0; k < back.data[i].subMenus[j].subMenus.length; k++) {
								if (back.data[i].subMenus[j].subMenus[k].url != "") {
									url = back.data[i].subMenus[j].subMenus[k].url;
									theme = look.getStringParam("theme", url) ? look.getStringParam("theme", url) : "";
									type = look.getStringParam("type", url) ? look.getStringParam("type", url) : "";
									dream = look.getStringParam("dream", url) ? look.getStringParam("dream", url) : "";
								}
								else {
									url = "javascript:";theme = "";type = "";dream = "";
								}
								menuHtml += '<li><a href="' + url + '" theme="' + theme + '" type="' + type + '" dream="' + dream + '">' + back.data[i].subMenus[j].subMenus[k].name + '</a></li>';
							}
							menuHtml += "</ul>";
						}
					}
				}
				menuHtml += "</ul>";
			}
			$("#navPanel ul").html(menuHtml);
			
			$(".look_tree").looktree();
			publicInit.menuSet();
			publicInit.menuDateSet("theme", "data");
		}
		
		if($("#navPanel ul").children().length == 0){
//			loadPage("#nav","menu.html");
//			$(".look_tree").looktree();
//			getMenuCallback();
			dataConn(look._IM10003.url, look._IM10003.getJson(), getMenuCallback);
		}
	},
	menuSet : function(){
		if(look.getHashParam("type")){
			$("#navPanel a").removeClass("selected");
			$("#navPanel a[theme='" + (look.getHashParam("theme") ? look.getHashParam("theme") : "") + "'][dream='" + (look.getHashParam("dream") ? look.getHashParam("dream") : "") + "'][type='" + (look.getHashParam("type") ? look.getHashParam("type") : "") + "']").addClass("selected").parents("ul").attr("show", true).show().prev("li").addClass("open");
		}
	},
	menuDateSet : function(element, string){
		//菜单设置日期
		var to = look.getLocalDate(),
			from = look.getLastMonthDate(to);
		$("#navPanel a").each(function(i){
			if($(this).attr("href") != "javascript:"){
				var hashString = $(this).attr("href").split("#")[1] ? $(this).attr("href").split("#")[1] : $(this).attr("href");
				if(look.getStringParam(element, hashString) == string){
					$(this).attr("href", "#" + look.setParam([["from", from], ["to", to]], hashString));
				}
			}
		});
	},
	layout : function(){
		var wWidth = parseInt($(window).width());
		var wHeight = parseInt($(window).height());
		var hHeight = parseInt($(".header").height());
		var fHeight = parseInt($(".footer").height());
		var contentHeight = wHeight - hHeight - fHeight;
		$("#main").css({height: contentHeight});
		$("#wrapper").css({height: contentHeight - $("#topBar").height()});
		$("#nav").css("height", contentHeight + 2);
		$("#navPanel").css("height", contentHeight - 20);

		$("#loader").css("left", (wWidth - 32) / 2);
	},
	setBreadcrumb : function(title){
		$("#breadcrumb .breadcrumb_cell").remove();
		var lastClass="";
		for(var i = 0; i < title.split(",").length; i++){
			$("#breadcrumb").append(
				'<span class="breadcrumb_cell">' +
				'	<a href="javascript:" class="breadcrumb_link">' + title.split(",")[i] + '<span class="breadcrumb_arrow"></span>' + '</a>' +
				'</span>'
			);
		}
		$("#breadcrumb .breadcrumb_arrow:last").addClass("last");
	},
	inputStyle : function(){
		$(".text,textarea").live("mouseenter", function(){
			$(this).addClass("hover");
			return false;
		}).live("mouseleave", function(){
			$(this).removeClass("hover");
			return false;
		}).live("focus", function(){
			$(this).addClass("selected").select();
		}).live("blur", function(){
			$(this).removeClass("selected");
		});
	},
	inputTipStyle : function(element, label){
		$(element).bind("focus",function(){
			var tip = $(this).parent().attr("tip");
			$(this).parent().next().removeClass("color_red").text(tip);
		}).bind("blur",function(){
			if($.trim($(this).val()) == ""){
				$(this).prev(label).show();
			}else{
				$(this).prev(label).hide();
			}
		}).live("keyup",function(){
			if($.trim($(this).val()) == $(this).prev(label).text() || $.trim($(this).val()) == ""){
				$(this).val("");
				$(this).prev(label).show();
			}else{
				$(this).prev(label).hide();
			}
		}).each(function(){
			if($.trim($(this).val()) != ""){
				$(this).prev(label).hide();
			}else{
				$(this).prev(label).show();
			}
			var tip = $(this).parent().attr("tip");
			$(this).parent().next().removeClass("color_red").text(tip);
		});
	},
	checkboxStyle : function(obj){
		$(obj + " .a_as_checkbox").unbind("click").bind("click", function(){
			$(this).toggleClass("selected");
		});
	},
	tabelStyle : function(){
		$("table").each(function(){
			$(this).find("tr:even").addClass("even");
		});
		$("table tr").unbind("mouseenter mouseleave").bind("mouseenter mouseleave", function(){
			$(this).toggleClass("hover");
		});
	},
	i18n : {
		ChangeManual : function(){
			$("#languageChangeSubset").bind("click", function(event){
				event.stopPropagation();
				$("#languageCell").toggle();
			});
			$("body").bind("click", function(){
				$("#languageCell").hide();
			});
			$("#languageCell a").bind("click", function(event){
				event.stopPropagation();
				$.cookie("lang", $(this).attr("lang"), {expires: 365});
				window.location.reload();
			});
		},
		GetResource : function(){
			var lang = $.cookie("lang");//look.getHashParam("lang") ? look.getHashParam("lang") : (navigator.userLanguage ? navigator.userLanguage : navigator.language)
			if(!lang || lang == ""){
				lang = navigator.userLanguage ? navigator.userLanguage : navigator.language;
				lang = look.hasString("en", lang) ? "en" : lang;
				$.cookie("lang", lang, {expires: 365});
			}
			if(!look._I18N_DATA || jQuery.isEmptyObject(look._I18N_DATA)){
				Look.prototype._I18N_DATA = new Object();
				Look.prototype._I18N_LANGUAGE = lang;
				$.ajax({
					url: look._I18N_RESOURCE_FOLD + "language_" + lang + ".json",
					dataType: "json",
					async: false,
					success: function(back, textStatus, XMLHttpRequest){
						look._I18N_DATA = back;
					},
					error: function(XMLHttpRequest, textStatus, errorThrown){
						look.showInfo.error(textStatus);
					}
				});
			}
		},
		Render : function(){
			if (look._I18N_DATA) {
				$("*[look-lang]").not(".rendered").each(function(){
					var lang = $(this).attr("look-lang");
					$(this).addClass("rendered").text(look._I18N_DATA[lang]);
				});
			}
		},
		languageMatch : function(lang){
			switch(lang){//zh-Hans, en, ko, ja, zh-Hant
				case "zh-cn" : return "zh-Hans";
					break;
				case "zh-tw" : return "zh-Hant";
					break;
				case "en" : return "en";
					break;
				case "ko" : return "ko";
					break;
				case "ja" : return "ja";
					break;
				default : return "en";
			}
		}
	},
	backTop : function(right){
		var backTopSubset = $('#backTopSubset');
		$("#wrapper").unbind("scroll").bind("scroll", function() {
			if ($(this).scrollTop() > 10) {
				backTopSubset.removeClass("disabled");
			} else {
				backTopSubset.addClass("disabled");
			}
		});

		backTopSubset.unbind("click").bind("click", function() {
			if($(this).hasClass("disabled")){return false;}
			$('#wrapper').animate({
				scrollTop: 0
			}, 800);
			return false;
		});
	},
	zoom : function(string, callback){
		if(string == "disabled"){
			$("#zoomOutSubset, #zoomInSubset").addClass("disabled");
		}else {
			$("#zoomOutSubset, #zoomInSubset").removeClass("disabled");
		}
		$("#zoomOutSubset").unbind("click").bind("click", function(){
			if($(this).hasClass("disabled")){return false;}
			if (callback) {
				eval(callback("out"));
			}
		});
		$("#zoomInSubset").unbind("click").bind("click", function(){
			if($(this).hasClass("disabled")){return false;}
			if (callback) {
				eval(callback("in"));
			}
		});
	}
}
//国家化资源加载，要最先进行加载
publicInit.i18n.GetResource();